<?php
	class User_model extends CI_Model{
		//获取当前用户的所有信息
		function get_user($uid=-1){
			if ($uid==-1){
				$uid=$this->get_uid();
			}	
			if($uid!=0){
				//get_user_detail
				$this->db->select('*');
				$this->db->where('uid',$uid);
				$query=$this->db->get('ask_user');
				$result=$query->result_array();
				$this->db->select('grouptitle');
				$this->db->select('grouptype');
				$this->db->select('creditshigher');
				$this->db->where('groupid',$result[0]['groupid']);
				$query=$this->db->get('ask_usergroup');
				$result2=$query->result_array();
				$result[0]['grouptitle']=$result2[0]['grouptitle'];
				$result[0]['grouptype']=$result2[0]['grouptype'];
                $result[0]['creditshigher']=$result2[0]['creditshigher'];
				//get_newmsg
				$this->load->model('user/message_model');
				$result[0]['newmsg']=$this->message_model->get_newmsg_num($uid);
				//get upload num;
				$this->load->model('share/file');
				$result[0]['uploadNum']=$this->file->get_upload_times($uid);
				//get answer num;
				$this->db->select("id");
				$this->db->where("authorid",$uid);
				$query=$this->db->get("ask_question");
				$result[0]['answerNum']=$query->num_rows();
				//get recall num;
				$this->load->model('recall/recall_paper_model');
				$result[0]['recallNum']=$this->recall_paper_model->get_recall_times($uid);
				$this->load->library('global_func');
				$result[0]['avatar']=$this->global_func->get_avatar_dir($uid);
				return $result[0];
			}
		}
		//获得uid
		function get_uid(){
			$this->load->library('global_func');
			$sid=$this->global_func->tcookie('sid');
			if($sid==''){return 0;}
			$this->db->select('uid');
			$this->db->where('sid',$sid);
			$query=$this->db->get('ask_session');
			$row=$query->result_array();
			return $row ? $row[0]['uid'] : 0;
		}
		//通过uid获得username
		function get_user_name($uid){
			$this->db->select('username');
			$this->db->where('uid',$uid);
			$query=$this->db->get('ask_user');
			$row=$query->result_array();
			return $row ? $row[0]['username'] :"";
		}
		//通过username获得uid
		function get_user_uid($username){
			$this->db->select('uid');
			$this->db->where('username',$username);
			$query=$this->db->get('ask_user');
			$row=$query->result_array();
			return $row[0]['uid'];
		}
		//通过uid获得phone
		function get_user_phone($uid){
			$this->db->select('phone');
			$this->db->where('uid',$uid);
			$query=$this->db->get('dy_user');
			$row=$query->result_array();
			if($row[0]['phone']!=NULL)
				return $row[0]['phone'];			
			return 0;
		}
		
		//通过uid获得snum
		function get_user_snum($uid){
			$this->db->select('snum');
			$this->db->where('uid',$uid);
			$query=$this->db->get('dy_user');
			$row=$query->result_array();
			if($row[0]['snum']!=NULL)
				return $row[0]['snum'];
			return 0;
		}

		//返回用户上传历史，
		//返回一个二维数组
		function get_upload_history($uid,$limit=10,$offset=1){
			$this->db->select('fid,category,subject,title,rank,rank_times,download_times,upload_time');
			$this->db->where('uid',$uid);
			$query=$this->db->get('dy_upload',$limit,$offset);
			$row=$query->result();
			//print_r($row);
			return $row;
		}
		//获取uid对应用户的credits,包括credit1(相当于RP),credit2(因为问答和分享和回忆获得的RP)和groupid
		function get_credits($uid){
			if($uid<=0){return -1;}
			$this->db->select('credit1,credit2,groupid');
			$this->db->where('uid',$uid);
			$query=$this->db->get('ask_user');
			$row=$query->result_array();
			return $row[0];
		}	
		//更新$uid代表用户的RP记录--->credit1表示积分增加量，credit2表示用户回忆，上传，回答问题所得到的积分,operation为具体操作
		function refresh_rp($uid,$credit2=20,$operation='',$credit1=-1){
        	if(!$operation) $operation='user/register';
			if($credit1==-1){$credit1=$credit2;}
			if($uid<=0){return 0;}
			$data=array(
				'uid'=>$uid,
				'time'=>time(),
				'operation'=>$operation,
				'credit1'=>$credit1,
				'credit2'=>$credit2
			);	
			$this->db->insert('ask_credit',$data);
			$credits=$this->get_credits($uid);
			$data=array(
				'credit1'=>$credits['credit1']+$credit1,
				'credit2'=>$credits['credit2']+$credit2
			);	
			$this->db->where('uid',$uid);
        	$this->db->update('ask_user',$data);
        	if ($credits['groupid']>=7) {
            $currentcredit1=$credits['credit1']+$credit1;
			$this->db->select('groupid');
			$this->db->where('grouptype',2);
			$this->db->where('creditslower <=',$currentcredit1);
			$this->db->order_by("creditslower",'desc');
			$this->db->limit(1,0);
            $query=$this->db->get('ask_usergroup');
			$usergroup=$query->first_row();
            //判断是否需要升级
            if ($usergroup->groupid && ($credits['groupid']!=$usergroup->groupid) ) {
                $groupid = $usergroup->groupid;
				$data=array(
					'groupid'=>$groupid,
				);
				$this->db->where('uid',$uid);
                $this->db->update("ask_user",$data);
            }
        }
		return 1;
	}
	function list_by_credit($type=0,$limit=10 ) {
        $userlist = array();
        $starttime=0;
		//echo time();
        if(1==$type) {
            $starttime=time()-7*24*3600;
        }
        if(2==$type) {
            $starttime=time()-30*24*3600;
        }
        $sqlarray=array(
                "SELECT u.uid,u.groupid, u.username,u.gender,u.lastlogin,u.avatar,u.credit1 FROM ask_user  u ORDER BY `credit1` DESC LIMIT 0,$limit",
                "SELECT u.uid,u.groupid, u.username,u.gender,u.lastlogin,u.avatar,sum( c.credit1 ) credit1  FROM ask_user u,ask_credit c   WHERE u.uid=c.uid AND c.time>$starttime   GROUP BY u.uid ORDER BY credit1  DESC LIMIT 0,$limit",
                "SELECT u.uid,u.groupid, u.username,u.gender,u.lastlogin,u.avatar,sum( c.credit1 ) credit1  FROM ask_user u,ask_credit c   WHERE u.uid=c.uid AND c.time>$starttime   GROUP BY u.uid ORDER BY credit1  DESC LIMIT 0,$limit"
        );
        $query = $this->db->query($sqlarray[$type]);
		$result=$query->result_array();
		//print_r($result);
        foreach($result as $num => $user) {
            $user['gender']=(1==$user['gender'])?'男':'女';
			$this->load->library("global_func");
            $user['lastlogin']=$this->global_func->tdate($user['lastlogin']);
			$tem=$this->get_user();
			$user['grouptitle']= $tem['grouptitle'];
			$this->load->library('global_func');
	        $user['avatar']=$this->global_func->get_avatar_dir($user['uid']);
            $userlist[$num] = $user;
        }
        return $userlist;
    }
	function get_question()
	{
		//$this->db->from('ask_question');
		$this->db->order_by('time','desc');
		$query=$this->db->get('ask_question',5,0);
		return $query->result();		
	}
}
